<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function myWebSocket(url, params) {
            return new Promise((resolve, reject) => {
                const socket = new WebSocket(url)
                socket.onopen = () => {
                    console.log('建立连接');
                    socket.send(JSON.stringify(params))
                }
                socket.onmessage = (res) => {
                    console.log(`从服务端接收数据:${res.data}`);

                    resolve(res)

                }

            })
        }

        myWebSocket('ws://localhost:3000', { age: 18 })

    </script>
</body>

</html>